package com.ogawa.projectcommon.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.ogawa.base.constants.Constants
import com.ogawa.projectcommon.R
import com.ogawa.projectcommon.activity.BaseMqttActivity
import kotlinx.android.synthetic.main.layout_time.view.*

class TimeView : FrameLayout, View.OnClickListener {
    private lateinit var activity: BaseMqttActivity
    private var func = Constants.TIME_ADJUST

    constructor(context: Context) : super(context)

    @SuppressLint("CustomViewStyleable")
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.layout_time, this)
        tv_1.setOnClickListener(this)
        tv_2.setOnClickListener(this)
        tv_3.setOnClickListener(this)
        tv_4.setOnClickListener(this)
        tv_5.setOnClickListener(this)
        tv_6.setOnClickListener(this)
        if (context is BaseMqttActivity) {
            activity = context
        }
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    fun setFunc(func: String) {
        this.func = func
    }

    @SuppressLint("SetTextI18n")
    fun setTime(time: Int) {
        tv_1.isSelected = (time in 1..5)
        tv_2.isSelected = (time in 6..10)
        tv_3.isSelected = (time in 11..15)
        tv_4.isSelected = (time in 16..20)
        tv_5.isSelected = (time in 21..25)
        tv_6.isSelected = (time > 25)
        var retTime = time
        if (retTime <= 0){
            retTime = 0
        }
//        tv_1.isSelected = time == 5
//        tv_2.isSelected = time == 10
//        tv_3.isSelected = time == 15
//        tv_4.isSelected = time == 20
//        tv_5.isSelected = time == 25
//        tv_6.isSelected = time == 30
        tv_lev.text = retTime.toString() + context.getString(R.string.minute)
    }

    override fun onClick(v: View?) {
        if (!activity.isSnExist()) {
            return
        }
        when (v?.id) {
            R.id.tv_1 -> activity.publish(func, "3", "5")
            R.id.tv_2 -> activity.publish(func, "3", "10")
            R.id.tv_3 -> activity.publish(func, "3", "15")
            R.id.tv_4 -> activity.publish(func, "3", "20")
            R.id.tv_5 -> activity.publish(func, "3", "25")
            R.id.tv_6 -> activity.publish(func, "3", "30")
        }
    }
}